package D12_6z.思考题;

import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;

public class Demo1 {
    public static void main(String[] args) throws Exception{
        Object o = run1();
        run2(o);
    }
    public static Object run1() throws Exception {
        Class<?> aClass = Class.forName("Y2019.M12_.D12_6z.思考题.Person");
        Constructor<?> decl = aClass.getDeclaredConstructor(String.class, int.class, String.class);
        decl.setAccessible(true);
        Object o = decl.newInstance("小张", 18, "130302201110010001");
        Method toString = aClass.getDeclaredMethod("toString");
        System.out.println(toString.invoke(o,null));
        return o;
    }
    public static void run2(Object o) throws Exception {
        String string = o.toString();
        int index = 0;
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]=='['){
                index = i;
            }
        }
        string = string.substring(index);
        string = string.replace("[", "");
        string = string.replace("]", "");
        string = string.replace(" ", "");
        System.out.println(string);
        Properties prop = new Properties();
        String[] split = string.split(",");
        for (int i = 0; i < split.length; i++) {
            prop.setProperty(split[i].split("=")[0],split[i].split("=")[1]);
        }
        FileWriter fw = new FileWriter("src/Y2019/M12_/D12_6z/思考题/data2.properties");
        prop.store(fw,"xxx");
        fw.close();
    }
}
